<template>
    <div style="width: 500px;">
        <el-input
                prefix-icon="el-icon-search"
                placeholder="请输入部门名称"
                v-model="filterText">
        </el-input>
        <el-tree
                :expand-on-click-node="false"
                :data="departments"
                :props="defaultProps"
                :filter-node-method="filterNode"
                ref="tree">
<!--            node代表当前的元素，data指元素里面的数据-->
             <span class="custom-tree-node" style="display: flex;justify-content: space-between;width:100%;" slot-scope="{ node, data }">
        <span>{{ data.name }}</span>
        <span>
          <el-button
                  type="primary"
                  size="mini"
                  class="treeButton"
                  @click="() => showAddDepView(data)">
           添加部门
          </el-button>
          <el-button
                  type="danger"
                  size="mini"
                  class="treeButton"
                  @click="() => deleteDep(data)">
            删除部门
          </el-button>
        </span>
      </span>
        </el-tree>
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <div style="display: flex;">
                    <el-tag>上级部门:</el-tag>
                    <span style="padding-top: 5px;padding-left: 5px;">{{pname}}</span>
                </div>
                <div>
                    <el-tag>部门名称:</el-tag>
                    <el-input style="width: 300px;margin-left: 5px;margin-top: 5px;" size="small" v-model="department.name" placeholder="请输入部门名称..."></el-input>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button size="small" @click="dialogVisible = false">取 消</el-button>
    <el-button size="small" type="primary" @click="addDepartment">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    import {Message} from "element-ui";

    export default {
        name: "DepManager",
        data() {
            return {
                dialogVisible:false,
                //搜索框输入的内容将会被赋值到这里
                filterText: '',
                department:{
                    name:'',
                    parentId:-1
                },
                pname:'',
                departments: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        mounted() {
            this.getDepartments();
        },
        methods:{
            removeDepartmentFromTree(p,departments,id){
                for (let i = 0; departments.length; i++) {
                    let d = departments[i];
                    if(d == undefined){
                        break;
                    }
                    if (d.id == id) {
                        departments.splice(i,1);
                        //如果移除点该节点后，它由父部门变成子部门了,则让它变成子部门
                        if(departments.length==0){
                            p.parent = false;
                        }
                        return;
                    } else {
                        this.removeDepartmentFromTree(d,d.children, id);
                    }
                }
            },
            deleteDep(data){
                if(data.parent){
                    Message.error({message: '该部门下面还含有子部门，无法删除！'});
                    return;
                }

                this.$confirm('此操作将永久删除【' + data.name + '】部门, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/department/" + data.id).then(resp => {
                        if (resp) {
                           this.removeDepartmentFromTree(null,this.departments,data.id);
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            showAddDepView(data){
                this.pname = data.name;
                this.department.parentId = data.id;
                this.dialogVisible = true;
            },
            //过滤函数,value为filter函数传入的搜索框内的值,data是指json数组中的对应的对象
            filterNode(value, data) {
                //返回true表示该节点可以显示
                if (!value) return true;
                //如果当前节点包含要查找的值，则保留这个节点，否则将隐藏这个节点
                return data.name.indexOf(value) !== -1;
            },
            getDepartments(){
                this.getRequest("/system/basic/department/").then(resp=>{
                    if(resp){
                        this.departments = resp;
                    }
                })
            },
            addDepartmentIntoTree(departments, department) {
                for (let i = 0; departments.length; i++) {
                    let d = departments[i];
                    if(d == undefined){
                        break;
                    }
                    if (d.id == department.parentId) {
                        d.children = d.children.concat(department);
                        //如果该部门添加了子部门，则变成父部门
                        if(d.children.length>0){
                            d.parent = true;
                        }
                        return;
                    } else {
                        this.addDepartmentIntoTree(d.children, department);
                    }
                }
            },
            addDepartment(){
                this.postRequest("/system/basic/department/",this.department).then(resp=>{
                    if(resp){
                        this.addDepartmentIntoTree(this.departments,resp.obj);
                        this.dialogVisible = false;
                        this.department={
                            name:'',
                            parentId:-1
                        };
                        this.pname = '';
                    }
                })
            }
        },
        watch: {
            //当搜索框内的值发送变化的时候，会调用树的filter方法，然后filter方法会调用filterNode()过滤函数
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        }
    }
</script>

<style>
    .treeButton{
        padding: 2px;
    }
</style>